package com.ruoyi.system.controller;

import java.nio.charset.Charset;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbInvoiceRequests;
import com.ruoyi.system.service.ITbInvoiceRequestsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发票申请Controller
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/system/requests")
public class TbInvoiceRequestsController extends BaseController
{
    @Autowired
    private ITbInvoiceRequestsService tbInvoiceRequestsService;

    /**
     * 查询发票申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:requests:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbInvoiceRequests tbInvoiceRequests)
    {
        startPage();
        Object company = ServletUtils.getRequest().getHeader("company");
        if(ObjectUtil.isNotNull(company))
        {
            tbInvoiceRequests.setCompany(URLDecoder.decode(company.toString(), Charset.defaultCharset()));
        }
        List<TbInvoiceRequests> list = tbInvoiceRequestsService.selectTbInvoiceRequestsList(tbInvoiceRequests);
        return getDataTable(list);
    }

    /**
     * 导出发票申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:requests:export')")
    @Log(title = "发票申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbInvoiceRequests tbInvoiceRequests)
    {
        List<TbInvoiceRequests> list = tbInvoiceRequestsService.selectTbInvoiceRequestsList(tbInvoiceRequests);
        ExcelUtil<TbInvoiceRequests> util = new ExcelUtil<TbInvoiceRequests>(TbInvoiceRequests.class);
        util.exportExcel(response, list, "发票申请数据");
    }

    /**
     * 获取发票申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:requests:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbInvoiceRequestsService.selectTbInvoiceRequestsById(id));
    }

    /**
     * 新增发票申请
     */
    @PreAuthorize("@ss.hasPermi('system:requests:add')")
    @Log(title = "发票申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInvoiceRequests tbInvoiceRequests)
    {
        return toAjax(tbInvoiceRequestsService.insertTbInvoiceRequests(tbInvoiceRequests));
    }

    /**
     * 修改发票申请
     */
    @PreAuthorize("@ss.hasPermi('system:requests:edit')")
    @Log(title = "发票申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInvoiceRequests tbInvoiceRequests)
    {
        return toAjax(tbInvoiceRequestsService.updateTbInvoiceRequests(tbInvoiceRequests));
    }

    /**
     * 删除发票申请
     */
    @PreAuthorize("@ss.hasPermi('system:requests:remove')")
    @Log(title = "发票申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbInvoiceRequestsService.deleteTbInvoiceRequestsByIds(ids));
    }
}
